package com.lrs.java.j2se_self._example.countWords;

import java.util.HashMap;
import java.util.Map;

public class WordsCount {
	public static final int ONE = 1;
	
	private String words;
	private Map<Character, Integer> map;
	
	public WordsCount() {
		map = new HashMap<Character, Integer>();
	}
	
	public WordsCount(String words) {
		this();
		this.words = words;
	}
	
	protected String getText() {
		return words;
	}
	
	public void count(WordsCount wc) {
		String str = wc.getText();
		char ch;
		str = str.replaceAll("\\s", "");    //除去字符串中包含的空格
		for(int i=0; i<str.length(); i++) {
			ch = str.charAt(i);
			
			if(map.get(ch) != null) {
				map.put(ch, map.get(ch) + 1);
			} else {
				map.put(ch, ONE);
			}
			
			/*if(!map.containsKey(ch)) {
				map.put(ch, ONE);
			} else {
				map.put(ch, map.get(ch) + 1);
			}*/
		}
	}

	public void display() {
		System.out.println(map);
	}
	
}
